package com.ftg.learn.rbac.mapper;

import com.ftg.learn.rbac.entity.Permission;
import com.ftg.learn.rbac.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.w3c.dom.stylesheets.LinkStyle;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author kangjx
 * @since 2022-03-23
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {

    /**
     * 然后我告诉你是否存在
     * @param account  账号
     * @param password 密码
     * @return
     */
    @Select("select uid, uaccount, uname from sys_user where uaccount=#{account} and upassword=#{password} and ustatus=0")
    User checkUser(@Param("account") String account, @Param("password")String password);

    @Update("update sys_user set ustatus=#{num} where uid = #{id}")
    int updateForUstatus(@Param("id") int id,@Param("num") int num);

    @Update("update sys_user set uaccount=#{uaccount},uname=#{uname} where uid = #{uid}")
    boolean updateUserInfo(@Param("uid")int uid, @Param("uaccount")String uaccount, @Param("uname")String uname);

    @Update("update sys_user set upassword=#{upassword} where uid = #{uid}")
    boolean resetPassword(@Param("uid")int uid,@Param("upassword") String upassword);

    /**
     * 当前uid的全部角色
     * @param uid
     * @return
     */
    @Select("select distinct p.* from sys_user_role ur " +
            " left join sys_role_permission rp on ur.rid = rp.rid " +
            " left join sys_permission p on rp.pid = p.id " +
            " where " +
            " ur.uid = #{uid}")
    List<Permission> allPermissionByUid(@Param("uid") Integer uid);
}
